package com.mpgame.core.status;

import java.util.Objects;

import com.fasterxml.jackson.databind.JsonNode;
import com.mpgame.core.model.Monster;

public abstract class StatusBase {
    private final StatusType type;
    private Monster mOwner, mOpponent;
    private int roundDuration;
    protected StatusBase(StatusType type, int roundDuration){
        this.type = type;
        this.roundDuration = roundDuration;
    }
    public StatusType getStatusType(){
        return this.type;
    }
    public StatusBase setMOwner(Monster owner){
        this.mOwner = owner;
        return this;
    }
    public StatusBase setMOpponent(Monster opponent){
        this.mOpponent = opponent;
        return this;
    }
    public void setRoundDuration(int value) {this.roundDuration = value;}
    public int getRoundDuration(){ return roundDuration; }
    /** 消耗 */
    public void Consume(){
        Take(mOwner, mOpponent);
        roundDuration--;
        if(roundDuration <= 0) mOwner.removeStatus(this);
    }
    protected abstract void parseJson(JsonNode params);
    protected abstract void Take(Monster mOwner, Monster mOpponent);
    @Override
    public final boolean equals(Object o){
        if(!(o instanceof StatusBase)) return false;
        StatusBase that = (StatusBase)o;
        return this.getStatusType() == that.getStatusType();
    }
    @Override
    public final int hashCode(){
        return Objects.hash(getStatusType());
    }

    public static StatusBase parseStatusJson(JsonNode statusNode){
        String type = statusNode.get("type").asText();
        JsonNode params = statusNode.get("params");
        switch(type.toLowerCase()){
            case "burn":
                BurnStatus burnStatus = new BurnStatus();
                burnStatus.parseJson(params);
                return burnStatus;
            case "continuous_damage":
                ContinuousDamageStatus continuousDamageStatus = new ContinuousDamageStatus();
                continuousDamageStatus.parseJson(params);
                return continuousDamageStatus;
            default:
                throw new IllegalArgumentException("未知状态类型：" + type);
        }
    }
}
